Skip to content

add support for basic findOne with ID#154

Open
intellix wants to merge 1 commit intomickhansen:masterfrom
intellix:dataloader-sequelize-find-one
Open

add support for basic findOne with ID#154
intellix wants to merge 1 commit intomickhansen:masterfrom
intellix:dataloader-sequelize-find-one

Conversation

@intellix
Copy link
Copy Markdown
Contributor

@intellix intellix commented Apr 24, 2026

following on from #147

I decided to see if Claude could help me fix the test, since this work is so valuable to us and it seems to have it all passing.

Supports dataloading for cases such as:

User.findOne({ where: { id: 1 } })
User.findOne({ where: { id: 2 } })
// → SELECT ... FROM users WHERE id IN (1, 2)

User.findOne({ where: { id: 1, status: 'active' } })
User.findOne({ where: { id: 3, status: 'active' } })
// → SELECT ... FROM users WHERE id IN (1, 3) AND status = 'active'

User.findOne({ where: { id: 2, status: 'inactive' } })
// → SELECT ... FROM users WHERE id IN (2) AND status = 'inactive'  (separate loader)

User.findOne({ where: { id: 1, [Op.and]: [{ deletedAt: null }] } })
User.findOne({ where: { id: 2, [Op.and]: [{ deletedAt: null }] } })
// → SELECT ... FROM users WHERE id IN (1, 2) AND deletedAt IS NULL

User.findOne({ where: { id: 1, enabled: true }, order: [['id', 'DESC']] })
User.findOne({ where: { id: 2, enabled: true }, order: [['id', 'DESC']] })
// → SELECT ... FROM users WHERE id IN (1, 2) AND enabled = true ORDER BY id DESC

User.findOne({ where: { id: 3, enabled: true }, order: [['id', 'ASC']] })
// → SELECT ... FROM users WHERE id IN (3) AND enabled = true ORDER BY id ASC  (separate)

Also, it adds a new BATCH_BY_ATTRIBUTE to easily batch by another attribute like a foreignKey:

import { EXPECTED_OPTIONS_KEY, BATCH_BY_ATTRIBUTE } from 'dataloader-sequelize';

Box.findOne({
  [EXPECTED_OPTIONS_KEY]: context.dataloaderContext,
  [BATCH_BY_ATTRIBUTE]: 'ancestorBoxId',
  where: { ancestorBoxId: 1, enabled: true },
});
Box.findOne({
  [EXPECTED_OPTIONS_KEY]: context.dataloaderContext,
  [BATCH_BY_ATTRIBUTE]: 'ancestorBoxId',
  where: { ancestorBoxId: 2, enabled: true },
});
// → SELECT * FROM boxes WHERE ancestorBoxId IN (1, 2) AND enabled = true

@intellix intellix force-pushed the dataloader-sequelize-find-one branch 9 times, most recently from 496214f to df251f1 Compare April 24, 2026 12:20
@intellix intellix force-pushed the dataloader-sequelize-find-one branch from df251f1 to 0f1cd4f Compare April 24, 2026 12:23
@intellix
Copy link
Copy Markdown
Contributor Author

let me know what you think @mickhansen - if there's anything I need to defend or test against and if you like the BATCH_BY_ATTRIBUTE stuff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant